<template>
	<view>
		<view class="goods-list">
			<view v-for="item,index in goodsList" :key="index" @click="toGoodsDetail(item)">
				<my-goods :goods="item"></my-goods>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 请求参数对象
				queryObj: {
					query: '',
					cid: '',
					pageNum: 1,
					pageSize: 10
				},
				goodsList: [],
				total: 0,
				isLoading: false

			};
		},
		methods: {
			async getGoodsList(cb) {
				this.isLoading = true
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
				if (res.meta.status !== 200) {
					return uni.$showMsg()
				}
				this.goodsList = [...this.goodsList, ...res.message.goods]
				this.total = res.message.total

				this.isLoading = false
				cb && cb()
			},
			toGoodsDetail(item) {
				uni.navigateTo({
					url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
				})
			}
		},
		onLoad(options) {

			this.queryObj.query = options.query || ''
			this.queryObj.cid = options.cid || ''

			this.getGoodsList()
		},
		onReachBottom() {

			if (this.queryObj.pageNum * this.queryObj.pageSize >= this.total) {
				return uni.$showMsg('没有数据了')
			}
			if (this.isLoading) return
			this.queryObj.pageNum++
			this.getGoodsList()

		},
		onPullDownRefresh() {
			this.queryObj.pageNum = 1
			this.queryObj.total = 0,
				this.isLoading = false,
				this.goodsList = []

			this.getGoodsList(() => {
				uni.stopPullDownRefresh()
			})
		}
	}
</script>

<style lang="scss">

</style>
